<?php

class Quad_View_Html extends Quad_View_Abstract {
	
	protected $_controller, $_layoutTplPath, $_contentTplPath;
	protected $_helpers = array();
	
	public static $defaultHelpers = array(
	  'extractKeywords' => array('Quad_View_Helper_Html', 'extractKeywords'),
	  'select'          => array('Quad_View_Helper_Html', 'select'),
	  'options'         => array('Quad_View_Helper_Html', 'options'),
	  'radios'          => array('Quad_View_Helper_Html', 'radios'),
	  'checkboxes'      => array('Quad_View_Helper_Html', 'checkboxes'),
	  'trs'             => array('Quad_View_Helper_Html', 'trs'),
	  'pager'           => array('Quad_View_Helper_Html', 'pager'),
	);
	
	public function __construct(Quad_Controller_Interface $controller) {
		$this->_controller = $controller;
		// set default layout
		$this->setLayoutTpl('default');
		// set default content template
		$controller = $this->getActionName();
		$action = $this->getControllerName();
    $this->setContentTpl("$controller/$action");
    // set default helpers
    foreach (self::$defaultHelpers as $method => $fn) {
    	$this->addHelper($method, $fn);
    }
	}
		
	public function url($controller = 'index', $action = 'index', $params = array()) {
		return $this->_controller->frontController->getRouter()->buildUrl($controller, $action, $params);
	}
	
  public function setLayoutTplPath($path) {
  	$this->_layoutTplPath = $path;
  }
  
  public function setContentTplPath($path) {
    $this->_contentTplPath = $path;
  }
  
  public function setLayoutTpl($name) {
    $this->_layoutTplPath = PATH_ROOT . "layouts/$name.phtml";
  }
  
  public function setContentTpl($controllerSlashAction) {
    $this->_contentTplPath = PATH_ROOT . "views/$controllerSlashAction.phtml";
  }
  
  public function addHelper($method, $fn) {
  	$this->_helpers[$method] = $fn;
  }
  
  // LIFO
  public function addHelperPath($path) {
  	array_unshift($this->_helperPaths, $path);
  }
  
	public function assign($p, $v = null) {
		if (is_array($p)) {
			foreach ($p as $p1 => $v1) {
				$this->$p1 = $v1;
			}
    } elseif ($p instanceof Quad_MagicProperties) {
      $this->assign($p->getProperties());			
			
		} elseif (is_object($p)) {
			$this->assign(get_obj_vars($p));
			
		} else {
		  $this->$p = $v;
		}
		return $this;
	}
	
	public function fetch($layoutTpl = null, $contentTpl = null) {
		if ($layoutTpl)  { $this->_layoutTplPath  = $layoutTpl;  }
		if ($contentTpl) { $this->_contentTplPath = $contentTpl; }
		
		ob_start();
    include($this->_layoutTplPath);
		return ob_get_clean();
	}
	
	public function render($tpl) {
    echo $this->fetch($tpl);
	}
	
	public function getContent($contentTpl = null) {
    if ($contentTpl) { $this->_contentTplPath = $contentTpl; }
    
		ob_start();
		include($this->_contentTplPath);
		return ob_get_clean();
	}
		
	public function __call($m, $a) {
		// don't provide getters and setters to protected properties as in Quad_Magic_Properties
		// first check through _helpers
		// then check through global function names
		if (isset($this->_helpers[$m])) {
			return call_user_func_array($this->_helpers[$m], $a);
		}
		if (is_callable($m)) {
			return call_user_func_array($m, $a);
		}
		throw new Quad_Exception("Unknown method or helper '$m'");
	}
	
}